<?php

namespace App\Imports;

use App\Models\Good;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class OrdersImport implements ToCollection
{
    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        $data = $collection->toArray();
        unset($data['0']);
        if (count($data) > 1) {
            $this->createData($data);
        }
    }

    // 具体导入业务代码
    public function createData($rows)
    {
        foreach ($rows as $key => $value) {
            //证券编号处理
            if(strlen($value[1])==4){
                $value[1] = '00'.$value[1];
            }elseif (strlen($value[1])==3){
                $value[1] = '000'.$value[1];
            }elseif (strlen($value[1])==2){
                $value[1] = '0000'.$value[1];
            }
            if($value[3]=="买入"){
                $this->doBuy($value);
            }else{
                $this->doSell($value);
            }
        }
    }

    /**
     * 执行买入
     */
    public function doBuy($data){
        $order_info = Order::query()->where('security_code',$data[1])->where('completion_date',$data[0])->first();
        if($order_info){
            $order_info->transaction_number =  $order_info->transaction_number+$data[6]; //成交数量
            $order_info->transaction_amount =  $order_info->transaction_amount+$data[8]; //成交金额
            $order_info->commission_amount  =  $order_info->commission_amount+$data[9]; //手续费
            $order_info->stamp_amount       =  $order_info->stamp_amount+$data[10]; //印花税
            $order_info->other_amount       =  $order_info->other_amount+$data[11]; //其他杂费
            $order_info->save();
        }else{
            $model = new Order();
            $model->security_code   = $data[1];
            $model->security_name   = $data[2];
            $model->operate         = $data[3];
            $model->completion_date = $data[0];
            $model->incurred_amount = $data[4];
            $model->current_amount  = $data[5];
            $model->transaction_number = $data[6];
            $model->average_amount     = $data[7];
            $model->transaction_amount = $data[8];
            $model->commission_amount  = $data[9];
            $model->stamp_amount       = $data[10];
            $model->other_amount       = $data[11];
            $model->contract_number    = $data[12];
            $model->deal_number        = $data[13];
            $model->trading_market     = $data[14];
            $model->buy_time           =$this->changeTime($data[0]);
            $model->save();
        }

    }

    /**
     * 执行卖出
     */
    public function doSell($data){
        $order_info = Order::query()->where('security_code',$data[1])->where('status',Order::STATUS_HOLD)->first();
        if($order_info) {
            $order_info->sell_transaction_number = $order_info->sell_transaction_number + $data[6]; //成交数量
            $order_info->sell_average_amount     = $data[7];
            $order_info->sell_transaction_amount = $order_info->sell_transaction_amount + $data[8]; //成交金额
            $order_info->commission_amount       = $order_info->commission_amount + $data[9]; //手续费
            $order_info->stamp_amount            = $order_info->stamp_amount + $data[10]; //印花税
            $order_info->other_amount            = $order_info->other_amount + $data[11]; //其他杂费
            $order_info->sell_time               = $this->changeTime($data[0]);  //卖出时间
            $order_info->win_lose_amount         = $order_info->sell_transaction_amount - $order_info->transaction_amount; //盈利
            $order_info->hold_day                = $this->twoTime($order_info->buy_time,$order_info->sell_time);
            //如果卖出的数量与买入数量相同则修改状态
            if ($order_info->sell_transaction_number == $order_info->transaction_number) {
                if ($order_info->hold_day > 10) {
                    $sell_reason = 3;
                } else {
                    if ($order_info->win_lose_amount > 0) {
                        $sell_reason = 1;
                    } else {
                        $sell_reason = 2;
                    }
                }
                $order_info->sell_reason = $sell_reason;
                $order_info->status      = Order::STATUS_SELL;
            }
            if($order_info->win_lose_amount>0){
                $order_info->is_win = 1;
            }else{
                $order_info->is_win = 2;
            }
            $win_lose_percent = round(($order_info->win_lose_amount/$order_info->transaction_amount)*100,3);
            $order_info->win_lose_percent = $win_lose_percent;
            $order_info->save();
        }

    }


    /**
     * 改变时间格式
     * @param $time
     * @return string
     */
    public function changeTime($time){
        $year  = substr($time,0,4);
        $month = substr($time,4,2);
        $day   = substr($time,6,2);
        return $year.'-'.$month.'-'.$day;
    }

    /**
     * 计算两个时间之间的天数
     */
    public function twoTime($day1,$day2){
        return ceil((strtotime($day2)-strtotime($day1))/86400);
    }
}
